package com.ruoyi.heima.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;


import com.ruoyi.heima.domain.News;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.util.StringUtil;
import com.ruoyi.heima.domain.vo.NewsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.heima.domain.News;
import com.ruoyi.heima.service.ITbNewsService;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.heima.domain.News;
import com.ruoyi.heima.service.ITbNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * newsController
 * 
 * @author ruoyi
 * @date 2025-06-21
 */
@RestController
@RequestMapping("/news")
public class TbNewsController extends BaseController
{
    @Autowired
    private ITbNewsService tbNewsService;

    /**
     * 查询news列表
     */
    @RequiresPermissions("heima:news:list")
    @GetMapping("/list")
    public TableDataInfo list(News news)
    {
        startPage();
        List<News> list = tbNewsService.selectTbNewsList(news);
        return getDataTable(list);
    }

    /**
     * 导出news列表
     */

    @PostMapping("/export")
    public void export(HttpServletResponse response, News tbNews)
    {
        List<News> list = tbNewsService.selectTbNewsList(tbNews);
        ExcelUtil<News> util = new ExcelUtil<News>(News.class);
        util.exportExcel(response, list, "news数据");
    }
    /**
     * 导出news列表
     */

    @PostMapping("/export/{newsIds}")
    public void exportById(HttpServletResponse response, News tbNews)
    {
        //根据newsid查询news列表
        List<News> list = tbNewsService.selectTbNewsListById(tbNews.getNewsId());
        //List<News> list = tbNewsService.selectTbNewsList(tbNews);
        ExcelUtil<News> util = new ExcelUtil<News>(News.class);
        util.exportExcel(response, list, "news数据");
    }


    /**
     * 获取news详细信息
     */
    @RequiresPermissions("heima:news:query")
    @GetMapping(value = "/{newsId}")
    public AjaxResult getInfo(@PathVariable("newsId") Long newsId)
    {
        return success(tbNewsService.selectTbNewsByNewsId(newsId));
    }

    /**
     * 新增news
     */
    @RequiresPermissions("heima:news:add")

    @PostMapping
    public AjaxResult add(@RequestBody News tbNews)
    {
        return toAjax(tbNewsService.insertTbNews(tbNews));
    }

    /**
     * 修改news
     */
    @RequiresPermissions("heima:news:edit")
    @PutMapping
    public AjaxResult edit(@RequestBody News tbNews)
    {

        return toAjax(tbNewsService.updateTbNews(tbNews));
    }

    /**
     * 删除news
     */
	@DeleteMapping("/{newsIds}")
    public AjaxResult remove(@PathVariable String newsIds)
    {
        tbNewsService.deleteTbNewsByNewsIds(newsIds);
        return AjaxResult.success();
    }
    /**
     * 发布news
     */
    @PostMapping("/{newsIds}")
    public AjaxResult publishNews(@PathVariable("newsIds") String newsIds) {
        return tbNewsService.publishNews(newsIds);
    }
}
